package com.quick.android.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 对于BaseAdapter的基本封装
 * 
 * @author quick
 * 
 * @param <T>
 */
public abstract class BaseListAdapter<T> extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<T> data;

	public BaseListAdapter(Context context) {
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
	}

	public void addData(ArrayList<T> list) {
		if (data == null) {
			data = list;
		} else {
			data.addAll(list);
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return data == null ? 0 : data.size();
	}

	@Override
	public T getItem(int position) {
		return data == null ? null : data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = bindView(mInflater);
		T item = getItem(position);
		bindData(position, item);
		return convertView;
	}

	/** 綁定Item布局 */
	public abstract View bindView(LayoutInflater inflater);

	/** 设置数据 */
	public abstract void bindData(int position, T item);
}
